package cn.acmoj.oj.service;

import cn.acmoj.oj.bean.Problem;
import cn.acmoj.oj.dao.IContestProblemDao;
import cn.acmoj.oj.dao.IProblemDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * cn.acmoj.oj.service
 * Springer
 * 2017-12-24
 */
@Service
public class ProblemSetService {

    @Resource
    private IProblemDao dao;
    @Resource
    private IContestProblemDao iContestProblemDao;

    public List<Problem> getAll(String title, int page, int num) {
        Map<String, Object> param = new HashMap<>();
        if (title != null && title.isEmpty()) {
            param.put("title", title);
        }
        param.put("num", num);
        int start = (page - 1) * num;

        param.put("start", start);
        return dao.selectByParam(param);
    }

    public List<Problem> getAllContestProblem(int contest_id) {
        return dao.selectByContestId(contest_id);
    }

    public Problem getContestProblem(int contest_id, int num) {
        return dao.selectOnceByContestId(contest_id, num);
    }

    public int getContestProblemCount(int contest_id) {
        return iContestProblemDao.getCount(contest_id);
    }

    public List<Problem> getAllNotAdmin(String title, int page, int num) {
        Map<String, Object> param = new HashMap<>();
        param.put("defunct", 'N');
        if (title != null && title.isEmpty()) {
            param.put("title", title);
        }
        param.put("num", num);
        int start = (page - 1) * num;
        param.put("start", start);
        return dao.selectByParam(param);
    }

    public int getAllCount(String title) {
        Map<String, Object> param = new HashMap<>();
        param.put("title", title);
        return dao.selectCountByParam(param);
    }

    public int getAllNotAdminCount(String title) {
        Map<String, Object> param = new HashMap<>();
        param.put("defunct", 'N');
        param.put("title", title);
        return dao.selectCountByParam(param);
    }
}
